<?php
/**
 * Created by PhpStorm
 * User: Jhinwy
 * Date: 7/24/24
 * Time: 10:23 AM
 */


namespace app\adminapi\lists\template;


use app\adminapi\lists\BaseAdminDataLists;
use app\common\model\template\Template;
use app\common\lists\ListsSearchInterface;


/**
 * Template列表
 * Class TemplateLists
 * @package app\adminapi\lists
 */
class TemplateLists extends BaseAdminDataLists implements ListsSearchInterface
{


    /**
     * @notes 设置搜索条件
     * @return \string[][]
     * @author likeadmin
     * @date 2024/01/29 19:06
     */
    public function setSearch(): array
    {
        return [
            '=' => ['position', 'status'],
        ];
    }


    /**
     * @notes 获取列表
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * @author likeadmin
     * @date 2024/01/29 19:06
     */
    public function lists(): array
    {
        return Template::where($this->searchWhere)->where($this->queryWhere())
            ->field(['id', 'image', 'position', 'status', 'create_time'])
            ->limit($this->limitOffset, $this->limitLength)
            ->order(['id' => 'desc'])
            ->select()
            ->toArray();
    }


    /**
     * @notes 搜索条件
     * @author 段誉
     * @date 2023/2/24 16:08
     */
    public function queryWhere()
    {
        $where = [];

        // 创建时间
        if (!empty($this->params['start_time']) && !empty($this->params['end_time'])) {
            $time    = [strtotime($this->params['start_time']), strtotime($this->params['end_time'])];
            $where[] = ['rt.create_time', 'between', $time];
        }

        return $where;
    }


    /**
     * @notes 获取数量
     * @return int
     * @author likeadmin
     * @date 2024/01/29 19:06
     */
    public function count(): int
    {
        return Template::where($this->searchWhere)->count();
    }

}
